﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Collections.ObjectModel;
using Microsoft.Phone.Shell;

namespace Yelper.Phone {
    public partial class MainPage : PhoneApplicationPage {
        public MainPage() {
            InitializeComponent();
            PivotControl.ItemsSource = YelperStatic.App.PivotItems;
            YelperStatic.App.LoginStatusChanged += new EventHandler<EventArgs<LoginStatus>>(App_LoginStatusChanged);
        }

        private void App_LoginStatusChanged(object sender, EventArgs<LoginStatus> e) {
            Deployment.Current.Dispatcher.BeginInvoke(() => {
                if (e.Value == LoginStatus.LoggedIn) {
                    ((ApplicationBarMenuItem)ApplicationBar.MenuItems[0]).Text = "Sign out";
                    ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = true;
                } else {
                    ((ApplicationBarMenuItem)ApplicationBar.MenuItems[0]).Text = "Sign in";
                    ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = false;
                }
            });
        }
		private void NewYelpApplicationBarIconButton_Click(object sender, System.EventArgs e) {
			NavigationService.Navigate(new Uri("/NewYelp", UriKind.Relative));
		}		
		private void RefreshApplicationBarIconButton_Click(object sender, System.EventArgs e) {
            switch (PivotControl.SelectedIndex) {
                case 0:
                    LoadPublicTimeline();
                    break;
                case 1:
                    LoadMyTimeline();
                    break;
                case 2:
                    LoadMyMentions();
                    break;
                default:
                    break;
            }
		}

		private void AboutApplicationBarMenuItem_Click(object sender, System.EventArgs e) {
			NavigationService.Navigate(new Uri("/About", UriKind.Relative));
		}

		private void SignOutApplicationBarMenuItem_Click(object sender, System.EventArgs e) {
            if (YelperStatic.App.LoginStatus == LoginStatus.LoggedIn) {
                YelperStatic.App.Logout();
            } else {
                NavigationService.Navigate(new Uri("/Login", UriKind.Relative));
            }            
		}

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) {
            LoadPublicTimeline();
        }

        private void LoadPublicTimeline() {
            YelperStatic.App.LoadPublicTimelineAsync();
        }
        private void LoadMyTimeline() {
            YelperStatic.App.LoadMyTimelineAsync();
        }
        private void LoadMyMentions() {
            YelperStatic.App.LoadMyMentionsAsync();
        }
    }
}